
// ------------------------------------------------------------------------------
pulse.project._colourFileName = function(sFileName) {
	var parts = sFileName.split('.');
	var sColouredPath = '<b>' + parts[0] + '</b><i>';
	for (i = 1; i < parts.length; ++i) {
		sColouredPath += '.' + parts[i];
	}
	sColouredPath += '</i>';

	return sColouredPath;
}

// ------------------------------------------------------------------------------
pulse.project._processAttributes = function(attr) {
	if (attr.tagName == 'file') {
		attr.text = pulse.project._colourFileName(attr.name);
		attr.iconCls = pulse.content.getFileIconClass(attr.name);
		attr.leaf = true;
	} else if (attr.tagName == 'dir') {
		attr.text = attr.name;
		attr.iconCls = 'iconFolder';
		attr.loaded = true;
	} else if (attr.tagName == 'project') {
		attr.text = attr.name;
		attr.iconCls = 'iconProject';
		attr.loaded = true;
		attr.hidden = true;
		attr.expanded = true;
	}
}

// ------------------------------------------------------------------------------
pulse.project._ProjectLoader = Ext.extend(Ext.ux.XmlTreeLoader, {
			processAttributes : pulse.project._processAttributes,
			onCreateNode : function( oNode ) { pulse.todo('image preview'); pulse.image.TooltipPreview( oNode/*.getUI().getTextEl()*/, 'test/test.jpg'/*pulse.project.getProjectRelativePathFromFileNode( oNode )*/ ); }
		});
		//pulse.image.TooltipPreview( 'test/test.jpg');

// ------------------------------------------------------------------------------
pulse.project.getProjectRelativePathFromFileNode = function(node) {
	var sPath = node.attributes.name;

	var parentNode = node.parentNode;
	while (parentNode != null) {
		if (parentNode.attributes.tagName == "dir"
				|| parentNode.attributes.tagName == "project") {
			sPath = parentNode.attributes.name + "/" + sPath;
			parentNode = parentNode.parentNode;
		} else
			parentNode = null;
	}
	return sPath;
};

// ------------------------------------------------------------------------------
pulse.project._onProjectTreeDblClick = function(node, e) {
	if (node.attributes.tagName == "file") {
		pulse.content.loadFile(pulse.project
				.getProjectRelativePathFromFileNode(node));
	}
};

// ------------------------------------------------------------------------------
pulse.project._rename = function(oNode) {
	var sPath = pulse.project.getProjectRelativePathFromFileNode(oNode);
	var sName = oNode.attributes.name;

	var onSuccess = function(oResp) {
		oNode.attributes.name = oResp.argument.sNewName;
		if (oNode.attributes.tagName == 'file')
			oNode.setText(pulse.project
					._colourFileName(oResp.argument.sNewName));
		else
			oNode.setText(oResp.argument.sNewName);
	}

	var onFailure = function(oResp) {
		pulse.error('Cannot rename : ' + sPath);
	}

	var fMsgBoxCallback = function(sBtn, sNewName) {
		if (sBtn == 'ok') {
			bOk = false;
			if (oNode.attributes.tagName == 'file')
				bOk = pulse.file.isFileNameValid(sNewName, true);
			else
				bOk = pulse.file.isDirNameValid(sNewName, true);
			if (bOk) {
				var oCallbackData = {
					success : onSuccess,
					failure : onFailure,
					argument : {
						'sNewName' : sNewName
					}
				}
				pulse.content.rename(oCallbackData, sPath, sNewName);
			}
		}
	}

	Ext.MessageBox.show({
				title : 'Rename ' + sName,
				msg : 'What\'s the new name ?',
				width : 300,
				buttons : Ext.MessageBox.OKCANCEL,
				prompt : true,
				multiline : false,
				fn : fMsgBoxCallback,
				value : sName,
				icon : Ext.MessageBox.QUESTION
			});
}

// ------------------------------------------------------------------------------
pulse.project._removeFileOrDir = function(oNode) {
	var sPath = pulse.project.getProjectRelativePathFromFileNode(oNode);
	var sName = oNode.attributes.name;

	var onSuccess = function(oResp) {
		if (oNode.parentNode != null)
			oNode.remove();
	}
	var onFailure = function(oResp) {
		pulse.error('Cannot remove : ' + sName);
	}

	var fMsgBoxCallback = function(sBtn) {
		if (sBtn == 'ok') {
			var oCallbackData = {
				success : onSuccess,
				failure : onFailure,
				argument : {
					'sPath' : sPath
				}
			}
			pulse.content.removeFileOrDir(oCallbackData, sPath);
		}
	}

	Ext.MessageBox.show({
				title : 'Remove ' + sName,
				msg : 'Are you sure you want to remove ' + sName + ' ?',
				width : 400,
				buttons : Ext.MessageBox.OKCANCEL,
				fn : fMsgBoxCallback,
				icon : Ext.MessageBox.QUESTION
			});
}

// ------------------------------------------------------------------------------
pulse.project._fileContextMenu = new Ext.menu.Menu({
	items : [{
				id : 'download',
				text : 'Download',
				iconCls : 'iconDownload',
				handler : function(item) {
					alert(item.id);
				}
			}, {
				id : 'rename',
				text : 'Rename',
				handler : function(item) {
					pulse.project._rename(item.parentMenu.contextNode);
				}
			}, {
				id : 'remove',
				text : 'Remove',
				iconCls : 'iconRemove',
				handler : function(item) {
					pulse.project._removeFileOrDir(item.parentMenu.contextNode);
				}
			}]
});

// ------------------------------------------------------------------------------
pulse.project._refresh = function(oProjectNode) {
	var sUrl = pulse.server.getCommandUrl('sync_project', 'dir='
					+ oProjectNode.attributes.name);
	var oCallbackData = {
		success : function(resp) {
			oProjectNode.getLoader().load(oProjectNode.parentNode, null);
		},
		failure : function(resp) {
			pulse.error('Cannot refresh project '
					+ oProjectNode.attributes.name);
		}
	}
	pulse.server.sendRequest(sUrl, oCallbackData);
}

// ------------------------------------------------------------------------------
pulse.project._projectContextMenu = new Ext.menu.Menu({
			items : [{
						id : 'refresh',
						text : 'Refresh',
						iconCls : 'iconRefresh',
						handler : function(item) {
							pulse.project._refresh(item.parentMenu.contextNode);
						}
					}, {
						id : 'new-dir',
						text : 'New folder',
						iconCls : 'iconAdd',
						handler : function(item) {
							pulse.project
									._addNewDir(item.parentMenu.contextNode);
							item.parentMenu.contextNode.expand();
						}
					}, {
						id : 'rename',
						text : 'Rename'
					}]
		});

// ------------------------------------------------------------------------------
pulse.project._addNewFile = function(oDirNode) {
	var sDirPath = this.getProjectRelativePathFromFileNode(oDirNode);

	var onSuccess = function(oResp) {
		var oAttributes = {
			tagName : 'file',
			name : oResp.argument.sFileName
		}
		pulse.project._processAttributes(oAttributes);
		var oNewNode = new Ext.tree.TreeNode(oAttributes);
		oDirNode.appendChild(oNewNode);
		pulse.content.loadFile(oResp.argument.sFilePath);
	}

	var onFailure = function(oResp) {
		pulse.error('Cannot add a new file : ' + oResp.argument.sFilePath);
	}

	// MsgBox callback
	var fMsgBoxCallback = function(sBtn, sName) {
		if (sBtn == 'ok') {
			if (pulse.file.isFileNameValid(sName, true)) {
				var sFilePath = sDirPath + '/' + sName;
				var oCallbackData = {
					success : onSuccess,
					failure : onFailure,
					argument : {
						'sFileName' : sName,
						'sFilePath' : sFilePath
					}
				}
				pulse.content.createFile(oCallbackData, sFilePath, null);
			}
		}
	}

	Ext.MessageBox.show({
				title : 'Create a new file in ' + sDirPath,
				msg : 'What\'s the name of the new file ?',
				width : 300,
				buttons : Ext.MessageBox.OKCANCEL,
				prompt : true,
				multiline : false,
				fn : fMsgBoxCallback,
				icon : Ext.MessageBox.QUESTION
			});
}

// ------------------------------------------------------------------------------
pulse.project._addNewDir = function(oDirNode) {
	var sDirPath = this.getProjectRelativePathFromFileNode(oDirNode);

	var onSuccess = function(oResp) {
		var oAttributes = {
			tagName : 'dir',
			name : oResp.argument.sDirName
		}
		pulse.project._processAttributes(oAttributes);
		var oNewNode = new Ext.tree.TreeNode(oAttributes);
		oDirNode.appendChild(oNewNode);
	}

	var onFailure = function(oResp) {
		pulse.error('Cannot add a new folder : ' + oResp.argument.sNewDirPath);
	}

	// MsgBox callback
	var fMsgBoxCallback = function(sBtn, sName) {
		if (sBtn == 'ok') {
			if (pulse.file.isDirNameValid(sName, true)) {
				var sNewDirPath = sDirPath + '/' + sName;
				var oCallbackData = {
					success : onSuccess,
					failure : onFailure,
					argument : {
						'sDirName' : sName,
						'sNewDirPath' : sNewDirPath
					}
				}
				pulse.content.createDir(oCallbackData, sNewDirPath);
			}
		}
	}

	Ext.MessageBox.show({
				title : 'Create a new folder in ' + sDirPath,
				msg : 'What\'s the name of the new folder ?',
				width : 300,
				buttons : Ext.MessageBox.OKCANCEL,
				prompt : true,
				multiline : false,
				fn : fMsgBoxCallback,
				icon : Ext.MessageBox.QUESTION
			});
}

// ------------------------------------------------------------------------------
pulse.project._upload = function(oNode) 
{
	pulse.assert( oNode.attributes.tagName == 'dir' );
	
	var sDirPath 		= this.getProjectRelativePathFromFileNode(oDirNode);
	var oWinChooser = pulse.window.UploadFileChooser;

	oWinChooser.show();
}

// ------------------------------------------------------------------------------
pulse.project._dirContextMenu = new Ext.menu.Menu({
	items : [{
				id : 'new-file',
				text : 'New file',
				iconCls : 'iconAdd',
				handler : function(item) {
					pulse.project._addNewFile(item.parentMenu.contextNode);
					item.parentMenu.contextNode.expand();
				}
			}, {
				id : 'new-dir',
				text : 'New folder',
				iconCls : 'iconAdd',
				handler : function(item) {
					pulse.project._addNewDir(item.parentMenu.contextNode);
					item.parentMenu.contextNode.expand();
				}
			}, {
				id : 'add',
				text : 'Upload',
				iconCls : 'iconUpload',
				handler : function(item) {
					pulse.project._upload(item.parentMenu.contextNode);
					item.parentMenu.contextNode.expand();
				}
			}, {
				id : 'rename',
				text : 'Rename',
				handler : function(item) {
					pulse.project._rename(item.parentMenu.contextNode);
				}
			}, {
				id : 'download',
				iconCls : 'iconDownload',
				text : 'Download the directory'
			}, {
				id : 'remove',
				text : 'Remove',
				iconCls : 'iconRemove',
				handler : function(item) {
					pulse.project._removeFileOrDir(item.parentMenu.contextNode);
				}
			}]
});

// ------------------------------------------------------------------------------
pulse.project.createProjectTree = function() {
	var projectTree = new Ext.tree.TreePanel({
		el : 'ProjectPanel',
		useArrows : true,
		autoScroll : true,
		animate : false,
		enableDD : true,
		ddScroll : true,
		rootVisible : false,
		root : new Ext.tree.AsyncTreeNode(),
		loader : new pulse.project._ProjectLoader({
					dataUrl : 'content/test/project.xml'
				}),
		listeners : {
			render : function(tree) {
				tree.on("dblClick", pulse.project._onProjectTreeDblClick, null);
			},
			contextmenu : function(node, e) {
				node.select();
				var c;
				if (node.attributes.tagName == "file")
					c = node.getOwnerTree().fileContextMenu;
				else if (node.attributes.tagName == "dir")
					c = node.getOwnerTree().dirContextMenu;
				else
					c = node.getOwnerTree().projectContextMenu;
				c.contextNode = node; // register for further access
				c.showAt(e.getXY());
			}
		},
		projectContextMenu : pulse.project._projectContextMenu,
		dirContextMenu : pulse.project._dirContextMenu,
		fileContextMenu : pulse.project._fileContextMenu
	});
	projectTree.render();
	projectTree.getRootNode().expand();
};